package com.example.ktapplication.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.example.ktapplication.utils.toJson
import com.example.ktapplication.utils.toPx
import kotlin.math.max

class FlowLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr) {

    private var itemCallBack: (Any) -> Unit = {}

    fun addItemCallBack(callBack: (Any) -> Unit = {}) {
        itemCallBack = callBack
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)

        var heightUsed = 0
        var lineHeight = 0
        var lineWidth = 0

        for (i in 0 until childCount) {
            val child = getChildAt(i)
            if (child.visibility == View.GONE) continue

            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, heightUsed)
            val lp = child.layoutParams as MarginLayoutParams
            val childWidth = child.measuredWidth + lp.leftMargin + lp.rightMargin
            val childHeight = child.measuredHeight + lp.topMargin + lp.bottomMargin

            // 换行
            if (lineWidth + childWidth > widthSize) {
                heightUsed += lineHeight
                lineWidth = 0
                lineHeight = 0
            }

            lineWidth += childWidth
            lineHeight = max(lineHeight, childHeight)
        }

        heightUsed += lineHeight

        // 设置最终测量的宽高
        val finalWidth = if (widthMode == MeasureSpec.EXACTLY) widthSize else lineWidth
        val finalHeight = heightUsed
        setMeasuredDimension(finalWidth, finalHeight)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        val width = right - left

        var x = 0
        var y = 0
        var lineHeight = 0

        for (i in 0 until childCount) {
            val child = getChildAt(i)
            if (child.visibility == View.GONE) continue

            val lp = child.layoutParams as MarginLayoutParams
            val childWidth = child.measuredWidth
            val childHeight = child.measuredHeight

            // 换行
            if (x + childWidth + lp.leftMargin + lp.rightMargin > width) {
                x = 0
                y += lineHeight
                lineHeight = 0
            }

            // 布局子视图
            val cl = x + lp.leftMargin
            val ct = y + lp.topMargin
            val cr = cl + childWidth
            val cb = ct + childHeight
            child.layout(cl, ct, cr, cb)

            x += childWidth + lp.leftMargin + lp.rightMargin
            lineHeight = max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin)
        }
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context, attrs)
    }

    override fun generateDefaultLayoutParams(): LayoutParams {
        return MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
    }

    override fun generateLayoutParams(p: LayoutParams?): LayoutParams {
        return MarginLayoutParams(p)
    }

    fun addSearchItem(view: View, obj: Any, margin: Int = 10) {
        val lp = MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        lp.marginEnd = margin.toPx()
        lp.topMargin = margin.toPx()
        view.layoutParams = lp

        view.tag = obj
        view.setOnClickListener {
            itemCallBack.invoke(obj)
        }

        addView(view)
    }
}





